package com.xuyiwei.mayi.revoer;

import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;

/**
 * Created by xuyiwei on 2019/4/22.
 */
public class Producer {
    public static void   W(String[] args) throws MQClientException {
        //rmq-group 这个是集群分组，一般都用于集群分组接受消息 对生产者没影响
        DefaultMQProducer producer = new DefaultMQProducer("rmq-group");
        producer.setNamesrvAddr("192.168.66.171:9876;192.168.66.172:9876");
        producer.setInstanceName("producer");
        producer.start();
        try {
            for (int i = 0; i < 10; i++) {
                Thread.sleep(1000); // 每秒发送一次MQ
                //topic是主题，tag是该主题下更细化的一个分类
                Message msg = new Message("itmayiedu-topic", // topic 主题名称
                        "TagA", // tag 临时值
                        ("itmayiedu-"+i).getBytes()// body 内容
                );
                msg.setKeys(System.currentTimeMillis()+"");
                SendResult sendResult = producer.send(msg);
                System.out.println(sendResult.toString());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        producer.shutdown();
    }

}
